<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>

<script type="text/javascript">
    $(document).ready(function() {
        var ajaxLoading = false;
        $("input[name='saveDefaults']").live("click", function() {
            if (!ajaxLoading) {
                $("#waiting").show();
                ajaxLoading = true;
                var defParamValue = "";
                var selectedModel = $("input[name='selectedModel']").val();
                var checkedParam = false;
                $("input[name='eachDefault']").each(function(index) {
                    if ($(this).attr("checked")) {
                        checkedParam = true;
                        var paramId = $(this).parent().find("input[name='eachDefaultId']").val();
                        var parValue = $(this).parent().find(".defaultValue").val();
                        var eachParam = paramId + "@" + parValue;

                        if (defParamValue != "") {
                            defParamValue += "," + eachParam;
                        } else {
                            defParamValue = eachParam;
                        }
                    }
                });
                if (!checkedParam) {
                    alert("To save you need to select some parameters!!!");
                } else {
                    if (confirm("Are you shure that the values entered right ?")) {
                        $.ajax({
                            url: "setDefault.action",
                            data:{
                                defParamValues:defParamValue,
                                selectedModel:selectedModel
                            },
                            type:"post",
                            success:function(result) {

                                if (result == "success") {
                                    alert("Process has been completed successfully!");
                                    $.ajax({
                                        url: "viewModelParams.action",
                                        data:{
                                            selectedModel:selectedModel
                                        },
                                        type:"post",
                                        success:function(data) {
                                            $("#modelsParams").html(data);
                                        }
                                    });
                                } else if (result == "error") {
                                    alert("Sorry error occurred.Please try again!");
                                }
                            }
                        });
                    }
                }
                ajaxLoading = false;
                $("#waiting").hide();
            }
        });
    });
</script>
<style type="text/css">

    .defaults {
        width: 290px;
        float: left;
    }

    #saveDefaults {
        margin-top: 50px;
        width: 900px;
        float: left;
    }

    #grayBox {
        z-index: 100;
    }
</style>
<div id="waiting"></div>
<div style="margin-left: 350px;">
    <s:text name="label.modelParams"/>
</div>
<br/>

<s:if test="%{!isParamsEmpty()}">
    <s:iterator value="parameters" var="parameter" status="s_parameter">
        <div class="defaults">
            <input type="checkbox" name="eachDefault">
            <s:property value="#parameter.visualName"/>
            <s:if test="%{#parameter.defparam==1}">
                <img src="<%=request.getContextPath()%>/images/settings/default.png" alt="default" width="15"
                     height="15"/>
            </s:if>
            <br/>
            <s:if test="%{#parameter.writable==0}">
                <input type="text" value="<s:property value="#parameter.defaultValue" />" class="defaultValue"
                       disabled="disabled"/>
            </s:if>
            <s:else>
                <input type="text" value="<s:property value="#parameter.defaultValue" />" class="defaultValue"/>
            </s:else>
            <input type="hidden" value="<s:property value="#parameter.id" />" name="eachDefaultId"/>
        </div>
    </s:iterator>

    <input type="hidden" name="selectedModel" value="<s:property value='selectedModel' />"/>

    <div id='saveDefaults'>
        <div style="padding-left: 350px;float:left;width:900px;">
            <input type='submit' value='Save' name='saveDefaults'/>
        </div>

        <div style="color:red;font-size:13px;margin:20px 10px;float:left;">
            If you check the parameters and don't enter the values in the appropriate fields or enter
            wrong values and save them then devices will start working not correctly!!!
        </div>

    </div>
</s:if>
<s:else>
    <s:text name="msg.nothaving.params"/>
</s:else>

